﻿using Abp.EntityFrameworkCore;
using Abp.EntityFrameworkCore.Configuration;
using Abp.Modules;
using Abp.Reflection.Extensions;
using Batman.Cms.Core;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;

namespace Batman.Cms
{
    [DependsOn(typeof(AbpEntityFrameworkCoreModule),
        typeof(BatmanCoreModule))]
    public class CmsModule : AbpModule
    {
        private IConfiguration _configuration;

        public CmsModule(IConfiguration configuration)
        {
            _configuration = configuration;
        }

        public override void PreInitialize()
        {
            Configuration.Modules.AbpEfCore().AddDbContext<CmsDbContext>(options =>
            {
                if (options.ExistingConnection != null)
                {
                    options.DbContextOptions.UseSqlServer(options.ExistingConnection);
                }
                else
                {
                    var connectionString = _configuration.GetConnectionString("DefaultConnection");
                    options.DbContextOptions.UseSqlServer(connectionString);
                }
            });
        }

        public override void Initialize()
        {
            IocManager.RegisterAssemblyByConvention(typeof(CmsModule).GetAssembly());
        }
    }
}
